package aiRequestHandler

import (
	"AiSpeaker/constant"
	"AiSpeaker/internal/aiSpeakerOperate"
	"AiSpeaker/internal/domain"
	config "AiSpeaker/pkg/conf"
	"AiSpeaker/pkg/logger"
	"fmt"
)

type commonHandler struct {
}

func (c *commonHandler) commonReceiveHandler(aiBoxRequest *domain.AiBoxRequest, aiType string, speakerId string) {
	//获取算法配置
	key := fmt.Sprintf(constant.AlgorithmKeyFormat, speakerId, aiType, aiBoxRequest.Data.Type)
	algorithm := config.GlobalMapAiSpeakerConfig[key]
	if algorithm == nil {
		logger.Infof("没有找到对应音柱的算法配置信息，音柱id:[%s]，算法大类:[%s]，detailKey=[%d]", speakerId, aiType, aiBoxRequest.Data.Type)
		return
	}
	logger.Infof("speaker = %s, server = %s\n", algorithm.Speaker, algorithm.Server)

	//获取音柱token
	token, err := aiSpeakerOperate.Login(algorithm.Server, algorithm.User, algorithm.Passwd)
	if err != nil {
		logger.Errorf("token获取失败。server = %s, user = %s, passwd = %s", algorithm.Server, algorithm.User, algorithm.Passwd)
		return
	}

	//创建音柱任务
	taskId, err := aiSpeakerOperate.CreateTask(algorithm.Server, token, algorithm.DeviceId, algorithm.Audio, algorithm.AudioSecond, algorithm.Volume)
	if err != nil {
		return
	}

	//播放音柱任务的音频文件
	err = aiSpeakerOperate.ExecuteTask(algorithm.Server, token, taskId, algorithm.AudioSecond)
	if err != nil {
		return
	}
}
